package com.sgy.nio.buffer;

import java.nio.ByteBuffer;

/**
 * @author Administrator
 * @date 2025/8/24 16:20
 * @description 向缓冲区中添加数据
 */
public class PutBufferDemo {

    public static void main(String[] args) {
        ByteBuffer allocate = ByteBuffer.allocate(10);
        // 获取当前索引所在位置
        System.out.println(allocate.position());
        // 最多能操作到哪个索引
        System.out.println(allocate.limit());
        // 返回缓冲区总长度
        System.out.println(allocate.capacity());
        // 还有多少能操作的个数
        System.out.println(allocate.remaining());

        // 修改当前索引所在位置
        /*allocate.position(1);
        // 修改最多能操作到哪个索引的位置
        allocate.limit(9);
        System.out.println(allocate.position());
        System.out.println(allocate.limit());
        System.out.println(allocate.capacity());
        System.out.println(allocate.remaining());*/

        // 添加缓冲区数据
        allocate.put((byte) 97);
        System.out.println(allocate.position());
        System.out.println(allocate.limit());
        System.out.println(allocate.capacity());
        System.out.println(allocate.remaining());

        // 缓冲区添加数组
        allocate.put("abc".getBytes());
        System.out.println(allocate.position());
        System.out.println(allocate.limit());
        System.out.println(allocate.capacity());
        System.out.println(allocate.remaining());

        // 缓冲区添加数组
        allocate.put("123456".getBytes());
        System.out.println(allocate.position());
        System.out.println(allocate.limit());
        System.out.println(allocate.capacity());
        System.out.println(allocate.remaining());
        // 是否还能操作
        System.out.println(allocate.hasRemaining());
    }
}
